#include "head.h"
// 猜数字
void num(void)
{

    int num;
    srand(time(NULL));
    num = rand() % 100 + 1;
    int t;
    int min = 0;
    int max = 100;
    do
    {
        printf("请输入猜的数值");
        scanf("%d", &t);
        if (t < min || t > max)
        {
            printf("玩不起别玩\n");
            continue;
        }

        else if (t < num)
        {
            printf("小了\n");
            min = t;
            printf("范围在[%d-%d]之间", t, max);
        }
        else if (t > num)
        {
            printf("大了\n");
            max = t;
            printf("范围在[%d-%d]之间", min, t);
        }

    } while (t != num);
    if (t == num)
    {
        printf("nb,猜对了\n");
    }
    return;
}
// 九九乘法表

void nine()
{
    for (int row = 1; row <= 9; row++)
    {
        for (int col = 1; col <= row; col++)
        {
            printf("%d x %d = %d\t", col, row, col * row);
        }
        printf("\n"); // 每打印完一行后换行
        sleep(1);     // 延迟1秒
    }
}

// 计算器

void counter(void)
{
    double num1, num2;
    char x;
    double result;
    char n;
    // 提示用户输入
    do
    {
    
    

    printf("请输入算式: ");

    scanf("%lf %c %lf", &num1, &x, &num2);

    // 根据运算符进行相应的计算

    switch (x)
    {
    case '+':
        result = num1 + num2;
        printf("%.2lf + %.2lf = %.2lf\n", num1, num2, result);
        
        break;
    case '-':
        result = num1 - num2;
        printf("%.2lf - %.2lf = %.2lf\n", num1, num2, result);
       
        break;
    case '*':
        result = num1 * num2;
        printf("%.2lf * %.2lf = %.2lf\n", num1, num2, result);
        
        break;
    case '/':
        if (num2 != 0)
        {
            result = num1 / num2;
            printf("%.2lf / %.2lf = %.2lf\n", num1, num2, result);
            
           
        }
        else
        {
            printf("错误: 除数不能为零！\n");
           
            
        }
        break;

    default:
        printf("错误: 不支持的运算符！\n");
        
        break;
    }
     // 询问用户是否继续计算  
        printf("是否继续计算(y/n)?\n");  
        scanf(" %c", &n);  

    } while (n == 'y' || n == 'Y'); // 如果选择'y'继续循环  


 }

// 增加学生
void add_student(void)
{
    int d;

    u_list *list = init_register();
    F_scanf(list, FILE1);

    printf("请输入学生信息\n");
    printf("账号:\t");
    scanf("%d", &d);

    user_node *p = find_node_id(list, d);

    if (NULL != p)
    {
        printf("该账号已注册\n");
    }

    else
    {

        printf("该账号创建成功\n");
        user_data data_1;
        data_1.id = d;

        printf("密码\t");
        scanf("%s", data_1.password);

        printf("姓名\n");
        scanf("%s", data_1.name);

        insert_node(list, data_1, 1);
        print_register(list);

        F_print(list, FILE1);

        printf("添加成功\n");
    }
}

// 查找学生
void find(void)
{
    char n[128];

    u_list *list = init_register();
    printf("================================\n");

    printf("请输入要查询的学生姓名\n");
    scanf("%s", n);
    F_scanf(list, FILE1);
    // print_register(list);
    printf("================================\n");

    user_node *p = find_node_name(list, n);
    if (NULL != p)
    {
        printf("账号：%d\t", p->data.id);
        printf("姓名：%s\n", p->data.name);
    }
    else
    {
        printf("查无此人\n");
    }
}

// 删除学生
void delete(void)
{
    char name[128];
    u_list *list = init_register();
    printf("请输入要删除的学生姓名\n");
    F_scanf(list, FILE1);
    scanf("%s", name);
    find_node_name(list, name);
    delete_student(list, name);
    F_print(list, FILE1);
}
